// 项目的配置及webpakc的配置文件
// js代码使用严格模式
'use strict'
// nodejs的导包方式 遵守CommonJS
const path = require('path')
// 导入settings.js
const defaultSettings = require('./src/settings.js')

// 可以将传入的相对路径变成绝对路径
function resolve(dir) {
  // __dirname = /Users/apple/Desktop/heimahr/
  // dir = src/main.js
  // path.join('/Users/apple/Desktop/heimahr',src/main.js)
  // /Users/apple/Desktop/heimahr/src/main.js
  return path.join(__dirname, dir)
}

// 设置网页的标题
const name = defaultSettings.title || 'vue Admin Template' // page title

// 设置端口号
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// 对脚手架进行配置：  https://cli.vuejs.org/config/
module.exports = {

  // publicPath： 是设置导出的静态资源是否可以在本地打开，  / 在服务器的环境打开  './' 任何环境都可以打开
  publicPath: './',
  // 设置打包导出目录的名称
  outputDir: 'dist',
  // 设置打包导出目录里面静态文件目录的名称 static
  assetsDir: 'static',
  // 是否开启eslint检查
  lintOnSave: process.env.NODE_ENV === 'development',
  // 是否映射
  productionSourceMap: false,
  // devServer脚手架提供的服务器相关的配置
  devServer: {
    // 设置服务器的端口号
    port: port,
    // 浏览器是否自动打开
    open: true,
    // 设置浏览器的错误等级
    overlay: {
      warnings: false,
      errors: true
    },
    // 设置代理
    proxy: {
      // 会匹配以api开头的 自动拼接下面的指定地址
      '/api': {
        // target 指定的路径
        target: 'https://heimahr.itheima.net'
      }
    }
    // 加载mock服务  提供假数据 如果没有关闭请求会被mock拦截
    // before: require('./mock/mock-server.js')
  },
  // webpack的配置【对象配置】
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  // webpack的配置【函数配置【
  chainWebpack(config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // 配置svg  使用 svg-sprite-loader 打包成雪碧图一个大svg(可以缩放矢量图)
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        // 给symbol 设置一个id属性 ：前缀icon-svg文件名
        symbolId: 'icon-[name]'
      })
      .end()

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
            // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
